package com.zhs.gccs.view.gt;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.annimon.stream.Optional;
import com.annimon.stream.Stream;
import com.zhs.gccs.R;
import com.zhs.gccs.model.CheckBoxModel;
import com.zhs.gccs.model.FieldConfig;
import com.zhs.gccs.view.BaseView;
import com.zhs.gccs.vo.SysDictBean;

import java.util.ArrayList;
import java.util.List;

public class GbPropSelectView extends BaseView {
    private TextView tv_cancel;
    private TextView tv_confirm;
    private View ll_cb_all;
    private CheckBox cb_selected;
    private RecyclerView rv_items;
    private List<CheckBoxModel> gbPropList;
    private GbItemAdapter adapter;

    public GbPropSelectView(@NonNull Context context) {
        super(context);
    }

    public GbPropSelectView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public GbPropSelectView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_gb_prop,
                this, true);
        tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_confirm = view.findViewById(R.id.tv_confirm);
        ll_cb_all = view.findViewById(R.id.ll_cb_all);
        cb_selected = view.findViewById(R.id.cb_selected);
        rv_items = view.findViewById(R.id.rv_items);
        tv_cancel.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Stream.of(gbPropList).forEach(item->item.setSelected(false));
                cb_selected.setChecked(false);
                adapter.notifyDataSetChanged();
//                popupWindow.dismiss();
                if (dataChangeListener != null) dataChangeListener.dataChange();
            }
        });
        tv_confirm.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                if (dataChangeListener != null) dataChangeListener.dataChange();
                if (dataChangeListener != null) dataChangeListener.dismis();
            }
        });
        ll_cb_all.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                cb_selected.setChecked(!cb_selected.isChecked());
                Stream.of(gbPropList).forEach(item -> item.setSelected(cb_selected.isChecked()));
                adapter.notifyDataSetChanged();
                if (dataChangeListener != null) dataChangeListener.dataChange();
            }
        });
        this.gbPropList = new ArrayList<>();
//        rv_items.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_items.setLayoutManager(layoutManager);
        adapter = new GbItemAdapter(context, gbPropList);
        rv_items.addItemDecoration(new DividerItemDecoration(context
                , DividerItemDecoration.VERTICAL));
        rv_items.setAdapter(adapter);
        adapter.setOperationListener(new GbItemAdapter.OperationListener() {
            @Override
            public void onItemClick(int position) {
                CheckBoxModel checkBoxModel = gbPropList.get(position);
                checkBoxModel.setSelected(!checkBoxModel.isSelected());
                adapter.notifyItemChanged(position);
                long count = Stream.of(gbPropList).filter(item -> item.isSelected()).count();
                if(count==gbPropList.size()){
                    cb_selected.setChecked(true);
                }else {
                    cb_selected.setChecked(false);
                }
                if (dataChangeListener != null) dataChangeListener.dataChange();
            }
        });
    }

    public void initData(List<SysDictBean.SysDictItem> data) {
        Optional<SysDictBean.SysDictItem> first = Stream.of(data)
                .filter(i -> i.getDictLabel().equals("gb_props")).findFirst();
        if (first.isPresent()) {
            List<CheckBoxModel> boxModels = Stream.
                    of(JSON.parseArray(first.get().getDictValue(), FieldConfig.class))
                    .map(item -> {
                        CheckBoxModel model = new CheckBoxModel(item.getTitle(), item.getCode(), item.isChecked());
                        model.setFixed(item.isFixed());
                        model.setName(item.getName());
                        model.setAsc(item.isAsc());
                        model.setSort(item.isSort());
                        model.setType(item.getType());
                        model.setOrder(item.getOrder());
                        return model;
                    }).toList();
            gbPropList.addAll(boxModels);
            adapter.notifyDataSetChanged();
        }
        long count = Stream.of(gbPropList).filter(i -> i.isSelected()).count();
        boolean allSelected = count==gbPropList.size()?true:false;
        cb_selected.setChecked(allSelected);
    }
    public List<CheckBoxModel> getSelected() {
        return Stream.of(gbPropList).filter(i -> i.isSelected()).toList();
    }

    private DataChangeListener dataChangeListener;
    public void setDataChangeListener(DataChangeListener dataChangeListener) {
        this.dataChangeListener = dataChangeListener;
    }

    public interface DataChangeListener{
        void  dataChange();

        void dismis();
    }
}
